package com.pig4cloud.pig.module.controller.wx;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.wx.UserMerCoupon;
import com.pig4cloud.pig.module.api.entity.wx.WxUser;
import com.pig4cloud.pig.module.service.wx.IUserMerCouponService;
import com.pig4cloud.pig.module.service.wx.IWxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用户的商家抵扣券Controller
 *
 * @author ruoyi
 * @date 2023-03-02
 */
@Validated
@Api(value = "用户的商家抵扣券控制器", tags = {"用户的商家抵扣券管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/merCoupon")
public class UserMerCouponController {

    private final IUserMerCouponService userMerCouponService;
    private final IWxUserService wxUserService;

    @Value("${ruoyi.imgUrl}")
    private String imgUrl;

    /**
     * 查询用户的商家抵扣券列表
     */
    @ApiOperation("查询用户的商家抵扣券列表")
    @PreAuthorize("@pms.hasPermission('module:merCoupon:list')")
    @GetMapping("/list")
    public TableDataInfo<UserMerCoupon> list(@Validated UserMerCoupon userMerCoupon) {
        QueryWrapper<UserMerCoupon> qw = new QueryWrapper<>();
        if (userMerCoupon.getUserNo()!=null){
            WxUser wxUser1 = wxUserService.selectByUniqueColumn("user_no",userMerCoupon.getUserNo());
            if (wxUser1!=null){
                qw.eq("user_id",wxUser1.getId());
            }else {
                WxUser wxUser2 = wxUserService.selectByUniqueColumn("id",userMerCoupon.getUserNo());
                if (wxUser2!=null){
                    qw.eq("user_id",wxUser2.getId());
                }else {
                    qw.eq("user_id",0);
                }
            }
        }
        TableDataInfo<UserMerCoupon> td = PageUtils.buildDataInfo(userMerCouponService.page(PageUtils.buildPage(),qw));
        List<UserMerCoupon> list = td.getData();
        for (UserMerCoupon list1:list){
            WxUser wxUser = wxUserService.findById(list1.getUserId());
            if(wxUser!=null){
                list1.setNickName(wxUser.getNickName());
				list1.setFaceUrl(wxUser.getFaceUrl());
                if(StrUtil.isNotEmpty(wxUser.getFaceUrl()) && wxUser.getFaceUrl().indexOf("https") < 0){
                    list1.setFaceUrl(imgUrl + wxUser.getFaceUrl());
                }
            }
        }
        return td;
    }

    /**
     * 导出用户的商家抵扣券列表
     */
    @ApiOperation("导出用户的商家抵扣券列表")
    @PreAuthorize("@pms.hasPermission('module:merCoupon:export')")
    @SysLog("用户的商家抵扣券")
    @GetMapping("/export")
    public List export(@Validated UserMerCoupon userMerCoupon) {
        List<UserMerCoupon> list = userMerCouponService.list();
        // ExcelUtil<UserMerCoupon> util = new // ExcelUtil<UserMerCoupon>(UserMerCoupon.class);
        return list;// util.exportExcel(list, "用户的商家抵扣券");
    }

    /**
     * 获取用户的商家抵扣券详细信息
     */
    @ApiOperation("获取用户的商家抵扣券详细信息")
    @PreAuthorize("@pms.hasPermission('module:merCoupon:query')")
    @GetMapping("/{id}")
    public R<UserMerCoupon> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        UserMerCoupon userMerCoupon = userMerCouponService.getById(id);
        return R.ok(userMerCoupon);
    }

    /**
     * 新增用户的商家抵扣券
     */
    @ApiOperation("新增用户的商家抵扣券")
    @PreAuthorize("@pms.hasPermission('module:merCoupon:add')")
    @SysLog("用户的商家抵扣券")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody UserMerCoupon userMerCoupon) {
        return R.okByRow(userMerCouponService.save(userMerCoupon) ? 1 : 0);
    }

    /**
     * 修改用户的商家抵扣券
     */
    @ApiOperation("修改用户的商家抵扣券")
    @PreAuthorize("@pms.hasPermission('module:merCoupon:edit')")
    @SysLog("用户的商家抵扣券")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody UserMerCoupon userMerCoupon) {
        return R.okByRow(userMerCouponService.updateById(userMerCoupon) ? 1 : 0);
    }

    /**
     * 删除用户的商家抵扣券
     */
    @ApiOperation("删除用户的商家抵扣券")
    @PreAuthorize("@pms.hasPermission('module:merCoupon:remove')")
    @SysLog("用户的商家抵扣券")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(userMerCouponService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
